/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package east2west.dao;

import east2west.dto.TourCancelDTO;
import east2west.interfaces.TourCancelInterface;
import east2west.util.DatabaseUtil;
import east2west.util.DateTimeUtil;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.Date;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

/**
 *
 * @author thanhluan
 */
public class TourCancelDAO implements TourCancelInterface {

    Connection con = null;
    CallableStatement call = null;
    TourCancelDTO dto = null;

    public ArrayList<TourCancelDTO> readByAll() {
        ArrayList<TourCancelDTO> list = null;
        try {
            con = DatabaseUtil.getConnection();
            call = con.prepareCall("{call sp_Call_V_TourCancel_GetAll}");
            ResultSet rs = call.executeQuery();
            list = new ArrayList<TourCancelDTO>();
            while (rs.next()) {
                dto = new TourCancelDTO();
                dto.setTourCancelId(rs.getInt(1));
                dto.setOrderDetailId(rs.getInt(2));
                Date date = rs.getDate(3);
                if (date == null) {
                    dto.setCancelDate("");
                } else {
                    dto.setCancelDate(DateTimeUtil.convert2String(rs.getDate(3), "dd-MM-yyyy"));
                }
                dto.setDiscount(rs.getFloat(4));
                list.add(dto);
            }
            rs.close();
            call.close();
            return list;
        } catch (SQLException ex) {
            return null;
        }
    }

    public ArrayList<TourCancelDTO> findByCancelDate(String cancelDate) {
        ArrayList<TourCancelDTO> list = null;
        try {
            con = DatabaseUtil.getConnection();
            call = con.prepareCall("{call sp_Call_V_TourCancel_FindCancelDate(?)}");
            call.setDate(3, DateTimeUtil.convert2Date(cancelDate, "dd-MM-yyyy"));

            ResultSet rs = call.executeQuery();
            list = new ArrayList<TourCancelDTO>();
            while (rs.next()) {
                dto = new TourCancelDTO();
                dto.setTourCancelId(rs.getInt(1));
                dto.setTourCancelId(rs.getInt(2));
                Date date = rs.getDate(3);
                if (date == null) {
                    dto.setCancelDate("");
                } else {
                    dto.setCancelDate(DateTimeUtil.convert2String(rs.getDate(3), "dd-MM-yyyy"));
                }
                dto.setDiscount(rs.getFloat(4));

                list.add(dto);
            }
            rs.close();
            call.close();
            return list;
        } catch (SQLException ex) {

            return null;
        }
    }

    public TourCancelDTO findById(int id) {
        try {
            con = DatabaseUtil.getConnection();
            call = con.prepareCall("{call sp_Call_V_TourCancel_FindId(?)}");
            call.setInt(1, id);
            ResultSet rs = call.executeQuery();
            while (rs.next()) {
                dto = new TourCancelDTO();
                dto.setTourCancelId(rs.getInt(1));
                dto.setTourCancelId(rs.getInt(2));
                Date date = rs.getDate(3);
                if (date == null) {
                    dto.setCancelDate("");
                } else {
                    dto.setCancelDate(DateTimeUtil.convert2String(rs.getDate(3), "dd-MM-yyyy"));
                }
                dto.setDiscount(rs.getFloat(4));
            }
            rs.close();
            call.close();
            return dto;
        } catch (SQLException ex) {

            return null;
        }
    }

    public ArrayList<TourCancelDTO> findByTourCancelDto(TourCancelDTO dto) {
//        ArrayList<TourCancelDTO> list = null;
//        try {
//            con = DatabaseUtil.getConnection();
//            call = con.prepareCall("{call sp_Call_V_TourCancel_FindTourCancel(?)}");
//            call.setInt(1, dto.getTourCancelId());
//
//            ResultSet rs = call.executeQuery();
//            list = new ArrayList<TourCancelDTO>();
//            while (rs.next()) {
//                dto = new TourCancelDTO();
//                dto.setTourCancelId(rs.getInt(1));
//                dto.setOrderId(rs.getInt(2));
//                dto.setCancelDate(DateTimeUtil.convert2String(rs.getDate(3), "dd/mm/yyyy"));
//                dto.setDiscount(rs.getFloat(4));
//
//                list.add(dto);
//            }
//            rs.close();
//            call.close();
//            return list;
//        } catch (SQLException ex) {
//            try {
//                Logger.getLogger(HotelDAO.class.getName()).log(Level.SEVERE, null, ex);
//                if (con != null) {
//                    con.rollback();
//                }
//
//            } catch (SQLException ex1) {
//                Logger.getLogger(HotelDAO.class.getName()).log(Level.SEVERE, null, ex1);
//            }
//            return null;
//        } finally {
//            try {
//                if (con != null && !con.isClosed()) {
//                    DatabaseUtil.closeConnection(con);
//                }
//            } catch (SQLException ex) {
//                Logger.getLogger(HotelDAO.class.getName()).log(Level.SEVERE, null, ex);
//            }
//        }
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public boolean create(TourCancelDTO dto) {
        try {
            con = DatabaseUtil.getConnection();
            call = con.prepareCall("{call sp_Insert_TourCancel(?,?,?)}");
            call.setInt(1, dto.getOrderDetailId());
            call.setString(2, dto.getCancelDate());
            call.setFloat(3, dto.getDiscount());
            call.executeUpdate();
            call.close();
            return true;
        } catch (SQLException ex) {
            return false;
        } finally {
            try {
                if (con != null && !con.isClosed()) {
                    DatabaseUtil.closeConnection(con);
                }
            } catch (SQLException ex) {
            }
        }
    }

    public boolean update(TourCancelDTO dto) {
        try {
            dto = new TourCancelDTO();
            con = DatabaseUtil.getConnection();
            con.setAutoCommit(false);
            call = con.prepareCall("{call sp_Insert_TourCancel(?,?,?)}");
            call.setInt(1, dto.getTourCancelId());
            call.setInt(2, dto.getOrderDetailId());
            call.setDate(3, DateTimeUtil.convert2Date(dto.getCancelDate(), "dd-MM-yyyy"));
            call.setFloat(4, dto.getDiscount());
            call.executeUpdate();
            call.close();
            return true;
        } catch (SQLException ex) {
            try {

                if (con != null) {
                    con.rollback();
                }

            } catch (SQLException ex1) {
            }
            return false;
        } finally {
            try {
                if (con != null && !con.isClosed()) {
                    DatabaseUtil.closeConnection(con);
                }
            } catch (SQLException ex) {
            }
        }
    }

    public boolean delete(TourCancelDTO dto) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public boolean delete(int id) {
        try {
            con = DatabaseUtil.getConnection();
            call = con.prepareCall("{call sp_Delete_TourCancel(?)}");
            con.commit();
            call.setInt(1, id);
            call.executeUpdate();
            call.close();
            return true;
        } catch (SQLException ex) {
            try {

                if (con != null) {
                    con.rollback();
                }

            } catch (SQLException ex1) {
            }
            return false;
        } finally {
            try {
                if (con != null && !con.isClosed()) {
                    DatabaseUtil.closeConnection(con);
                }
            } catch (SQLException ex) {
            }
        }
    }

    public TourCancelDTO findByTourOrderDetailId(int id) {
        try {
            con = DatabaseUtil.getConnection();
            call = con.prepareCall("select * from V_TourCancel where TourOrderDetailId = ?");
            call.setInt(1, id);
            ResultSet rs = call.executeQuery();
            while (rs.next()) {
                dto = new TourCancelDTO();
                dto.setTourCancelId(rs.getInt(1));
                dto.setOrderDetailId(rs.getInt(2));
                dto.setCancelDate(DateTimeUtil.convert2String(rs.getDate(3), "MM/dd/yyyy"));
                dto.setDiscount(rs.getFloat(4));
            }
            rs.close();
            call.close();
            return dto;
        } catch (SQLException ex) {
            return null;
        } finally {
            DatabaseUtil.closeConnection(con);
        }
    }
}
